using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.IO;
using System.Drawing.Imaging;
using NikonGuyDesktopLib;


namespace NikonGuyPhotoDesktop
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>

  public partial class Window1 : System.Windows.Window
  {

    public ObservableCollection<string> ImageList
    {
      get { return _ImageList; }
      set { _ImageList = value; }
    } private ObservableCollection<string> _ImageList = new ObservableCollection<string>();

    System.Drawing.Image _Image;
 

    public Window1()
    {
      this.Loaded += new RoutedEventHandler(Window1_Loaded);

      InitializeComponent();      

      _AddImagesButton.Click += new RoutedEventHandler(_AddImagesButton_Click);
      _ClearListButton.Click += new RoutedEventHandler(_ClearListButton_Click);
      _ClearSelectedButton.Click += new RoutedEventHandler(_ClearSelectedButton_Click);
      _GenerateBackgroundButton.Click += new RoutedEventHandler(_GenerateBackgroundButton_Click);
      _SaveBackgroundButton.Click += new RoutedEventHandler(_SaveBackgroundButton_Click);
    }

    void _SaveBackgroundButton_Click(object sender, RoutedEventArgs e)
    {
      if (_Image != null)
      {
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Title = "Where do you want to save your background today?";
        saveFileDialog.Filter = "Image files (*.jpg, *.bmp, *.png, *.gif)|*.JPG;*.BMP;*.PNG;*.GIF|All files (*.*)|*.*";

        if (saveFileDialog.ShowDialog() == true)
        {
          ImageFormat imageFormat = ImageFormat.Jpeg;

          if (saveFileDialog.FileName.EndsWith(".bmp", StringComparison.InvariantCultureIgnoreCase))
          {
            imageFormat = ImageFormat.Bmp;
          }
          else if (saveFileDialog.FileName.EndsWith(".png",StringComparison.InvariantCultureIgnoreCase))
          {
            imageFormat = ImageFormat.Png;
          }
          else if (saveFileDialog.FileName.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase))
          {
            imageFormat = ImageFormat.Jpeg;
          }
          else if (saveFileDialog.FileName.EndsWith(".gif", StringComparison.InvariantCultureIgnoreCase))
          {
            imageFormat = ImageFormat.Gif;
          }
          else
          {
            saveFileDialog.FileName += ".jpg";
          }

          _Image.Save(saveFileDialog.FileName, imageFormat);
        }
      }
    }

    void _GenerateBackgroundButton_Click(object sender, RoutedEventArgs e)
    {
      GenerateDesktopBackground();
    }

    void _ClearSelectedButton_Click(object sender, RoutedEventArgs e)
    {
      ClearSelectedImages();
    }

    private void ClearSelectedImages()
    {
      string[] toRemove = new string[_ImageListBox.SelectedItems.Count];
      _ImageListBox.SelectedItems.CopyTo(toRemove, 0);

      foreach (string filename in toRemove)
      {
        _ImageList.Remove(filename);
      }
    }

    void _ClearListButton_Click(object sender, RoutedEventArgs e)
    {
      ClearImages();
    }

    private void ClearImages()
    {
      _ImageList.Clear();
    }

    void _AddImagesButton_Click(object sender, RoutedEventArgs e)
    {
      AddImages();
    }

    private void AddImages()
    {
      OpenFileDialog openFileDialog = new OpenFileDialog();

      openFileDialog.Title = "Select Images to Add to Your Background";
      openFileDialog.Multiselect = true;
      openFileDialog.Filter = "Image files (*.bmp, *.png, *.jpg, *.gif)|*.BMP;*.PNG;*.JPG;*.GIF|All files (*.*)|*.*";

      if (openFileDialog.ShowDialog() == true)
      {
        // add the image files to the list
        foreach (string filename in openFileDialog.FileNames)
        {
          _ImageList.Add(filename);
        }
      }
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
      
    }

    public void GenerateDesktopBackground()
    {
      int width = 640;
      int height = 480;
      int maxImageDimLength = 100;
      bool notFull = true;

      try
      {
        width = int.Parse(_BackgroundWidth.Text);
        height = int.Parse(_BackgroundHeight.Text);
        maxImageDimLength = int.Parse(_LargestImage.Text);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }

      // i think this is the wrong version of the code... i found it on my laptop
      Desktop3D d3d = new Desktop3D(new System.Drawing.Size(width, height), maxImageDimLength);

      _ProgressBar.Maximum = ImageList.Count;
      _ProgressBar.Value = 0;

      for (int i = 0; i < ImageList.Count && notFull; i++)
      {
        //_ProgressBar.Value = i;
        //_ProgressBar.InvalidateVisual();

        _ProgressBar.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, new System.Threading.ThreadStart(delegate
            {
              _ProgressBar.Value = i;
            }));  

        try
        {
          System.Drawing.Image newImage = System.Drawing.Image.FromFile(ImageList[i]);
          notFull = d3d.addImage(newImage);
        }
        catch (Exception ex)
        {
          Console.WriteLine(ex.ToString());
        }
      }

      _ProgressBar.Value = ImageList.Count;
      _ProgressBar.Value = 0;

      try
      {
        _Image = d3d.generateImage();
        MemoryStream ms = new MemoryStream();
        _Image.Save(ms, ImageFormat.Bmp);

        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = ms;
        bitmapImage.EndInit();

        _DesktopImageControl.Source = bitmapImage;
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }
    }

  }
}